import pexpect

def sshCmd (host, user, password, command):
    r=''
    ssh_newkey = 'Are you sure you want to continue connecting'
    child = pexpect.spawn('ssh -l %s %s %s'%(user, host, command),timeout=45)
    i = child.expect([pexpect.EOF, ssh_newkey, 'password: '])
    if i == 0:
        r="ssh failed"
        return r
    elif i == 1:
        child.sendline ('yes')
        i = child.expect([pexpect.EOF, 'password: '])
        if i == 0:
            r="ssh failed"
            return r
    child.sendline(password)
    r = child.read()
    return r

def scpCmd(host,src,dest,password,user='root'):
    r=''
    ssh_newkey = 'Are you sure you want to continue connecting'
    child=pexpect.spawn('scp %s %s@%s:%s' % (src,user,host,dest))
    i=child.expect([pexpect.EOF,ssh_newkey,'password: '])
    if i==0:
        r="scp failed"
        return r
    elif i==1:
        child.sendline('yes')
        i=child.expect([pexpect.EOF,'password:'])
        if i==0:
            r="scp failed"
            return r
    child.sendline(password)
    r=child.read()
    return r

